home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7181 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: 16 Feb 96 14:39:15 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.824481555@rscernix>
  8. References: <11f7cc$17261a.3b3@daprez> <9602011151.AA04526@dxmint.cern.ch> <4g040v$7jj@maverick.tad.eds.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4g040v$7jj@maverick.tad.eds.com> gulleha@vaxixhp3.vaxix.slg.eds.com (Harold Guller) writes:
  13.  
  14. >In article <9602011151.AA04526@dxmint.cern.ch>, danpop@mail.cern.ch (Dan Pop) says:
  15. >>>int main (int argc, char **argv) {
  16. >>>
  17. >>>  if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
  18. >>>    Usage();
  19. >>>  }
  20. >>...
  21. >>>
  22. >>>Can someone see what's wrong with this ?
  23. >>>am I not using strcmp() right ?
  24. >>
  25. >>Right :-)
  26. >>
  27. >>Your code is equivalent to:
  28. >>
  29. >>    if (strcmp(argv[1],"-d") == 0 || strcmp(argv[1],"-e") == 0) Usage();
  30. >>
  31. >>Which means that any time the program is correctly invoked, it will call
  32. >>Usage!
  33. >>
  34. >>So:
  35. >>
  36. >>1. Never use ! with strcmp.  It's a good way to shoot yourself in the foot.
  37. >
  38. >You can say that about anything that someone does not understad.
  39. >A better solution is to learn the concept.  The "!" operator is widely
  40. >used and should be understood.
  41.  
  42. Can't you read (and understand) a (fairly short) statement?  I didn't
  43. advocate against the usage of the ! operator, I did advocate against
  44. !strcmp().  This _is_ confusing even for people who understand both
  45. the ! operator and the strcmp function and can lead to errors unless
  46. the programmer is extremely careful.  Even if s/he is, the big point is
  47. that _conceptually_, it makes no sense to apply the ! operator to a
  48. function which doesn't return a "logical" value, even if the language
  49. allows this and the resulting code works fine. 
  50.  
  51. >>3. Are you sure you didn't mean && when you wrote ||?  Even if !strcmp()
  52. >>   would have worked as you thought it did, the || operator would have
  53. >>   caused the expression inside the if statement to _always_ evaluate to 1.
  54. >
  55. >Why do you advise learning the "&&" and "||" conditions?  These are more
  56. >complex than the "!" operator, and he did "shoot himself in the foot".
  57. >Shoudn't he use nested if else contrcuts to achieve his purpose?
  58.  
  59. Nested if-else constructs lead to more complex code which makes shooting
  60. yourself in the foot even easier.  All C operators should be understood
  61. and used _properly_.  The usage of ! in this example is a mistake, the 
  62. usage of the && operator is the right thing.
  63.  
  64. BTW, K&R2 introduces the && and || operators at page 21.  Way before the
  65. ! operator (page 41) or strcmp (page 106).
  66.  
  67. Dan
  68. --
  69. Dan Pop
  70. CERN, CN Division
  71. Email: danpop@mail.cern.ch 
  72. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  73.